package com.travel.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.travel.bean.Route;
import com.travel.bean.Ticket;
import com.travel.service.HotSpotService;
import com.travel.service.RouteService;
import com.travel.service.StrategyService;
import com.travel.service.TicketService;
import com.travel.utils.PageHelper;

@Controller
@RequestMapping(value={"/comm",""})
public class CommController {
	/**
	 * 展示旅游攻略
	 */
	@Resource(name="strategyServiceImpl")
	private StrategyService strategyService;
	/**
	 * 展示热门景点
	 */
	@Resource(name="hostSpotserviceImpl")
	private HotSpotService hostSpotservice;
	/**
	 * 展示票务列表
	 */
	@Resource(name="ticketServiceImpl")
	private TicketService ticketService;
	/**
	 * 展示旅游景点详情
	 */
	@Resource(name="routeServiceImpl")
	private RouteService routeService;
	
	
	/**
	 * 首页连接跳转到首页
	 * @return
	 */
	@RequestMapping("index.html")
	public String index(Map map){
		List<Map> strategys=strategyService.queryByPageIndex(null, 1, 3);
		List<Map> hotSpots=hostSpotservice.indexHotSpot();
		map.put("strategys", strategys);
		map.put("hotSpots",hotSpots);
		return "jsp/index";
	}
	/**
	 * 跳转到登录页面
	 * @return
	 */
	@RequestMapping("/tologin.html")
	public String toLogin(String path,Map map,Integer id){
		map.put("path", path);
		map.put("id", id);
		return "jsp/login";
		
	}
	/**
	 * 跳转到注册页面
	 * @return
	 */
	@RequestMapping("/toregister.html")
	public String toRegister(){
		return "jsp/register";
	}
	/**
	 * 到展示文章列表页面
	 * 传入将要显示的页码
	 * @return
	 */
	@RequestMapping("pagelist.html/{pageNum}")
	public String toPageList(@PathVariable Integer pageNum, Map map){
		PageHelper pageHelper=new PageHelper();
		pageHelper.setCurrentPageNo(pageNum);
		pageHelper.setPageSize(7);
		List<Map> list=strategyService.queryByPageIndex(null, pageHelper.getCurrentPageNo(), pageHelper.getPageSize());
		pageHelper.setTotalCount(strategyService.queryByPageCount(map));
		map.put("strategys", list);
		map.put("pageNum", pageHelper.getCurrentPageNo());
		map.put("pageTotal", pageHelper.getTotalPageCount());
		
		return "jsp/pagelist";
	}
	/**
	 * 跳转到展示票列表页面
	 * @return
	 */
	@RequestMapping("ticketlist.html/{pageNum}")
	public String tacketlist(@PathVariable Integer pageNum,Map map){
		PageHelper page=ticketService.tacketList(null, pageNum, 9);
		map.put("page", page);
		map.put("pageNum", page.getCurrentPageNo());
		map.put("pageTotal", page.getTotalPageCount());
		return "jsp/ticketlist";
	}
	/**
	 * 判断用户是否登录
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping("{path}/toorder.html/{id}")
	public String toOrder(@PathVariable String path,@PathVariable Integer id,HttpSession session){
		if(session.getAttribute("user")!=null){
			return "redirect:jsp/order/addoreder.html/"+id;
		}else{
			return "redirect:comm/tologin.html?path="+path+"&id="+id;
		}
	}
	
	/**
	 * 跳转路线详情页面
	 */
	@RequestMapping("/toroute.html/{id}")
	public String toroute(@PathVariable Integer id,Map map){
		Route route=routeService.getEntryById(id);
		map.put("route", route);
		return "jsp/routedetial";
	}
	
	/**
	 * 预定界面计算旅游价格
	 */
	@RequestMapping("/total.do")
	@ResponseBody
	public Map totalPrice(Integer routeId ,Integer childNum,Integer mamNum, Date date){
		Map jsonMap= new HashMap();
		String str=null;
		Double price =null;
		Ticket ticket=ticketService.getTicketByRouteId(routeId);
		if(ticket!=null){
		if(ticket.getStopTime().compareTo(date)<=0){
			str="亲，您选择的时间太迟，此路线停止旅游！";
		}else
		if(ticket.getLeftCount()<childNum+mamNum){
			str="对不起，票量余量不足！";
		}else{
			str="亲，你好，请稍等正在为您查询价格！";
			price=ticket.getPrice()*ticket.getDiscount()*childNum+ticket.getPrice()*ticket.getDiscount()*mamNum;
			}
		}
		
		jsonMap.put("msg", str);
		jsonMap.put("price", price);
		return jsonMap;
	}
	
	/**
	 * 进入后台管理界面
	 * @return
	 */
	@RequestMapping("admin.html")
	public String plans(){
		return "admin/index";
	}
	@RequestMapping("contact.html")
	public String contact(){
		return "jsp/contact";
	}
	
	
}
